import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import * as path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vueJsx from '@vitejs/plugin-vue-jsx'
import qiankun from 'vite-plugin-qiankun'
import viteCompression from 'vite-plugin-compression'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({ resolvers: [ElementPlusResolver()] }),
    Components({ resolvers: [ElementPlusResolver()] }),
    qiankun('sub-resourceBed', { useDevMode: true }),
    viteCompression({
      verbose: true, // 是否在控制台输出压缩结果
      disable: false, // 是否禁用
      threshold: 10240, // 体积大于 10KB 的文件才压缩
      algorithm: 'gzip', // 压缩算法
      ext: '.gz', // 压缩后缀
    }),
  ],
  base:
    process.env.NODE_ENV === 'development'
      ? '/'
      : 'http://115.190.202.175/resourcePool', // 强制设置
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
  resolve: {
    // 路径别名
    alias: {
      '@': path.resolve(__dirname, '.', './src/'),
    },
  },
  build: {
    outDir: 'dist_resourcePool',

    rollupOptions: {
      output: {
        // 更细粒度的分割
        manualChunks: (id) => {
          // 将 node_modules 分割
          if (id.includes('node_modules')) {
            if (id.includes('vue')) return 'vendor-vue'
            if (id.includes('react')) return 'vendor-react'
            if (id.includes('lodash')) return 'vendor-lodash'
            if (id.includes('echarts')) return 'vendor-echarts'
            return 'vendor-others'
          }

          // 将业务代码按模块分割
          if (id.includes('src/views/Home')) return 'chunk-Home'
          if (id.includes('src/views/Login')) return 'chunk-Login'
          if (id.includes('src/views/Recycle')) return 'chunk-Recycle'
          if (id.includes('src/views/User')) return 'chunk-User'
          if (id.includes('src/utils')) return 'chunk-utils'
        },
      },
    },
  },
  server: {
    // 热更新
    hmr: true,
    // 打开端口
    port: 7034,
    // // 代理
    proxy: {
      '/CSapi1': {
        target: 'http://127.0.0.1:7031',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/CSapi1/, ''),
      },
      '/CSapi2': {
        target: 'http://115.190.202.175:80/centenrSystemApi/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/CSapi2/, ''),
      },
      '/RBSapi1': {
        target: 'http://127.0.0.1:7033',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/RBSapi1/, ''),
      },
      '/RBSapi2': {
        target: 'http://115.190.202.175:80/resourcePoolApi/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/RBSapi2/, ''),
      },
    },
  },
})
